package com.mc.jf.controller;

import com.mc.jf.entity.HttpResponse;
import com.mc.jf.entity.Member;
import com.mc.jf.entity.PlanUser;
import com.mc.jf.entity.ResUtil;
import com.mc.jf.menu.*;
import com.mc.jf.service.IMemberService;
import com.mc.jf.service.impl.MemberServiceImpl;
import com.mc.jf.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.ListUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Lin
 * @since 2022-08-19
 */
@Controller
@RequestMapping("/")
public class IndexController {

    @Autowired
    AuthenticationManager authenticationManager;

    @Autowired
    RedisCache redisCache;

    PlanUser planUser;

    @GetMapping(value = "/")
    public String index() {
        return "index";
    }


    @RequestMapping("/menu")
    @ResponseBody
    public MenuDTO menu() {


        planUser = (PlanUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(planUser.getPermission());

        MenuDTO menu = new MenuDTO();

        HomeInfoDTO homeInfoDTO = new HomeInfoDTO();
        homeInfoDTO.setTitle("行程管理");
        homeInfoDTO.setHref("plan/index");
        menu.setHomeInfo(homeInfoDTO);

        LogoInfoDTO logoInfoDTO = new LogoInfoDTO();
        logoInfoDTO.setTitle("晟方国学");
        logoInfoDTO.setImage("images/logo.png");
        menu.setLogoInfo(logoInfoDTO);

        ArrayList<OutChildDTO> main_list = new ArrayList<>();

        if (hasAuth(AuthUtil.planEdit)) {
            OutChildDTO xcMu = new OutChildDTO();
            xcMu.setTitle("行程管理");
            xcMu.setIcon("fa fa-address-book");
            xcMu.setTarget("_self");
            ArrayList<InnerChildDTO> xc_list = new ArrayList<>();
            InnerChildDTO ixc = new InnerChildDTO();
            ixc.setTitle("行程管理");
            ixc.setHref("plan/index");
            ixc.setIcon("fa fa-file-text");
            ixc.setTarget("_self");
            xc_list.add(ixc);
            xcMu.setChild(xc_list);
            main_list.add(xcMu);
        }
        if (hasAuth(AuthUtil.bzIndex)) {
            OutChildDTO mlMu = new OutChildDTO();
            mlMu.setTitle("命理");
            mlMu.setIcon("fa fa-address-book");
            mlMu.setTarget("_self");
            ArrayList<InnerChildDTO> ml_list = new ArrayList<>();
            InnerChildDTO ibz = new InnerChildDTO();
            ibz.setTitle("八字");
            ibz.setHref("bazi/index");
            ibz.setIcon("fa fa-file-text");
            ibz.setTarget("_self");
            ml_list.add(ibz);

            InnerChildDTO itel = new InnerChildDTO();
            itel.setTitle("手机号");
            itel.setHref("tele/index");
            itel.setIcon("fa fa-file-text");
            itel.setTarget("_self");
            ml_list.add(itel);

            mlMu.setChild(ml_list);
            main_list.add(mlMu);
        }
        if (hasAuth(AuthUtil.jfEdit)) {
            OutChildDTO jfMu = new OutChildDTO();
            jfMu.setTitle("积分管理");
            jfMu.setIcon("fa fa-address-book");
            jfMu.setTarget("_self");
            ArrayList<InnerChildDTO> jf_list = new ArrayList<>();
            InnerChildDTO iqb = new InnerChildDTO();
            iqb.setTitle("全部会员");
            iqb.setHref("member/list");
            iqb.setIcon("fa fa-file-text");
            iqb.setTarget("_self");
            jf_list.add(iqb);
            InnerChildDTO ixz = new InnerChildDTO();
            ixz.setTitle("新增会员");
            ixz.setHref("member/add");
            ixz.setIcon("fa fa-file-text");
            ixz.setTarget("_self");
            jf_list.add(ixz);
            InnerChildDTO ibj = new InnerChildDTO();
            ibj.setTitle("会员编辑");
            ibj.setHref("member/edit");
            ibj.setIcon("fa fa-file-text");
            ibj.setTarget("_self");
            jf_list.add(ibj);
            InnerChildDTO ibg = new InnerChildDTO();
            ibg.setTitle("积分变更");
            ibg.setHref("member/score");
            ibg.setIcon("fa fa-file-text");
            ibg.setTarget("_self");
            jf_list.add(ibg);
            jfMu.setChild(jf_list);
            main_list.add(jfMu);
        }


        menu.setMenuInfo(main_list);

        return menu;
    }


    @GetMapping(value = "/login")
    public String login() {
        return "login";
    }


    @PostMapping("/login")
    @ResponseBody
    public Object login(String username, String password) {
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(username, password);
        Authentication authenticate = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        if (Objects.isNull(authenticate)) {
            throw new RuntimeException("验证登录失败");
        }
        PlanUser user = (PlanUser) (authenticate.getPrincipal());
        String jwt = JwtUtil.createJWT(user.getUsername());
        redisCache.setCacheObject("userid:" + user.getUsername(), FastJsonUtils.toJson(user));
        return ResUtil.ok(jwt);
    }


    @RequestMapping("/logot")
    @ResponseBody
    public Object logout() {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        PlanUser user = (PlanUser) authentication.getPrincipal();
        redisCache.deleteObject("userid:" + user.getUsername());
        return ResUtil.ok(null);
    }


    @GetMapping(value = "/fhplan")
    public String fhplan() {
        return "fhplan";
    }


    @GetMapping(value = "/plan")
    public String plan() {
        return "plan";
    }


    public boolean hasAuth(String perm) {
        if (planUser == null || ListUtils.isEmpty(planUser.getPermission())) {
            return false;
        }
        System.out.println(perm + "==========" + planUser.getPermission().contains(perm));
        return planUser.getPermission().contains(perm);
    }
}
